package com.sduept.nwld.dataserver.controller.message;

import com.sduept.bigdata.web.jsf.controller.AbstractController;
import com.sduept.component.message.model.CimEquipmentUpdateMessage;
import com.sduept.nwld.dataserver.manager.message.CimEquipmentUpdateMessageManager;
import com.sduept.utils.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.PostConstruct;
import javax.inject.Named;
import javax.faces.view.ViewScoped;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 一次设备更新消息管理
 *
 */
@Named
@ViewScoped
public class CimEquipmentUpdateMessageController extends AbstractController{

	
	/**
	 * 
	 */
	private static final long serialVersionUID = 6702649489627741106L;
	@Autowired
	private CimEquipmentUpdateMessageManager ceumm;
	
	private List<CimEquipmentUpdateMessage> messageList=new ArrayList<CimEquipmentUpdateMessage>();//全部消息列表
	private Date updateTimeStart = new Date();
	private Date updateTimeEnd = new Date();
	private String name;
	private List<CimEquipmentUpdateMessage> last3DaysMsgList = new ArrayList<CimEquipmentUpdateMessage>();

	@PostConstruct
	public void init() {
		//findAllMessage();
		findLast3DaysMessages();
	}
	/**
	 * 查询全部消息
	 */
	private void findAllMessage() {
		messageList = ceumm.findAllMessage();
	}
	
	/**
	 * 根据条件查询
	 */
	public void searchByCondition() {
		updateTimeStart = null==updateTimeStart?updateTimeStart:dateStartFormatter(updateTimeStart);
		updateTimeEnd = null==updateTimeEnd?updateTimeEnd:dateEndFormatter(updateTimeEnd);
		messageList = ceumm.searchByCondition(updateTimeStart, updateTimeEnd, name);
	}
	
	/**
	 * 清除查询条件
	 */
	public void clearCondition() {
		updateTimeStart = new Date();
		updateTimeEnd = new Date();
		name = null;
		findAllMessage();
	}
	
	/**
	 * 格式化日期为yyyy-MM-dd HH:mm:ss形式
	 * @param date
	 * @return
	 */
	private Date dateStartFormatter(Date date) {
		String dateStr=DateUtil.getStringDate(date, DateUtil.pattern_ymd000);
		return DateUtil.getDateByString(dateStr, DateUtil.pattern_ymdhms);
	}
	
	/**
	 * 格式化日期为yyyy-MM-dd HH:mm:ss形式
	 * @param date
	 * @return
	 */
	private Date dateEndFormatter(Date date) {
		String dateStr=DateUtil.getStringDate(date, DateUtil.pattern_ymd235959);
		return DateUtil.getDateByString(dateStr, DateUtil.pattern_ymdhms);
	}
	
	/**
	 * 根据消息的状态，返回不同值，供前台dataTable显示不同样式
	 * @param message
	 * @return
	 */
	public String getRowClass(CimEquipmentUpdateMessage message) {
		int status = message.getStatus();
		if(0 == status){
			return "unCheck";
		}else{
			return "checked";
		}
	}

	/**
	 * 获取最近三天更新的信息
	 */
	private void findLast3DaysMessages() {
		last3DaysMsgList = ceumm.findLast3DaysMessages(true);
	}
	public String getOperation(String operation) {
		String str = "";
		// 0：新增，1：更新，2：删除
		if("0".equals(operation)) {
			str = "新增";
		} else if("1".equals(operation)) {
			str = "更新";
		} else if ("2".equals(operation)) {
			str = "删除";
		}
		return str;
	}

	public List<CimEquipmentUpdateMessage> getMessageList() {
		return messageList;
	}
	
	public void setMessageList(List<CimEquipmentUpdateMessage> messageList) {
		this.messageList = messageList;
	}
	
	public Date getUpdateTimeStart() {
		return updateTimeStart;
	}
	
	public void setUpdateTimeStart(Date updateTimeStart) {
		this.updateTimeStart = updateTimeStart;
	}
	
	public Date getUpdateTimeEnd() {
		return updateTimeEnd;
	}
	
	public void setUpdateTimeEnd(Date updateTimeEnd) {
		this.updateTimeEnd = updateTimeEnd;
	}
	
	public String getName() {
		return name;
	}
	
	public void setName(String name) {
		this.name = name;
	}
	public List<CimEquipmentUpdateMessage> getLast3DaysMsgList() {
		return last3DaysMsgList;
	}
	public void setLast3DaysMsgList(List<CimEquipmentUpdateMessage> last3DaysMsgList) {
		this.last3DaysMsgList = last3DaysMsgList;
	}


}
